package com.lindroy.androidplayer.ui.home.fragment

import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.tabs.TabLayout
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseLazyFragment
import com.lindroy.androidplayer.extensions.no
import com.lindroy.androidplayer.extensions.parseState
import com.lindroy.androidplayer.extensions.setNetworkErrorLayout
import com.lindroy.androidplayer.extensions.yes
import com.lindroy.androidplayer.logic.model.ProjectTypesResp
import com.lindroy.androidplayer.ui.adapter.BaseTabAdapter
import com.lindroy.androidplayer.ui.adapter.ProjectTabAdapter
import com.lindroy.androidplayer.viewmodel.ProjectViewModel
import com.lindroy.lib_utils.extensions.setGone
import com.lindroy.lib_utils.extensions.setVisible
import kotlinx.android.synthetic.main.fragment_project.*
import kotlinx.android.synthetic.main.tablayout.*


/**
 * @author Lin
 * @date 2021/4/8
 * @function 首页-项目
 * @Description
 */
class ProjectFragment(override val layoutId: Int = R.layout.fragment_project) : BaseLazyFragment() {

    private val projectViewModel: ProjectViewModel by lazy { ViewModelProvider(this)[ProjectViewModel::class.java] }
    private val projectTypes = arrayListOf<ProjectTypesResp>()

    private val tabAdapter: ProjectTabAdapter by lazy {
        ProjectTabAdapter(projectTypes.map {
            BaseTabAdapter.TabBean(
                it.id,
                it.name
            )
        }, childFragmentManager)
    }

    override fun initView() {
        tabLayout.setupWithViewPager(vpProject)
        addLoadingObserve(projectViewModel)
    }

    override fun lazyLoad() {
        projectViewModel.reqProjectTypes()
    }

    override fun initListener() {

        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.let { vpProject.currentItem = it.position }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })
        projectViewModel.projectTypesLiveData.observe(this) { resultState ->
            parseState(resultState, onError = {
                tabLayout.setGone()
                stateLayout.setNetworkErrorLayout()
            }) {
                it.isNotEmpty().yes {
                    tabLayout.setVisible()
                    vpProject.offscreenPageLimit = it.size
                    stateLayout.showContent()
                    projectTypes.addAll(it)
                    vpProject.adapter = tabAdapter
                }.no {
                    tabLayout.setGone()
                    stateLayout.showEmpty()
                }
            }
        }
    }

    override fun initClick() {
        stateLayout.setOnViewsClickListener { state, view ->
            projectViewModel.reqProjectTypes()
        }
    }

    companion object {
        @JvmStatic
        fun newInstance() =
            ProjectFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }
}